package pl.edu.agh.transstat.displaying;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import pl.edu.agh.transstat.simulation.Configuration;

public class TableGenerator {

	private Map<String, Configuration> confs;
	public TableGenerator(Map<String, Configuration> confs) {
		this.confs = confs;
	}

	public void generate() {
		try {
			File comTableFile = new File("commisionsTable.txt");
			FileWriter fw = new FileWriter(comTableFile);
			fw.append("Commisions\n\n");
			fw.append("\tdynamic\tpackageSending\tchoosingByCost\tSTDepth\tworstCommission\talgorithm\tautoConfig\tfirstComplexSTResultOnly\n");
			for (Map.Entry<String, Configuration> conf: confs.entrySet()){
				Configuration c = conf.getValue();
				if (c.isPrint()){
	
					fw.append(conf.getKey() + "\t");
					
					Object val = c.getDynamic();
					fw.append(getStringValue(val) + "\t");
					val = c.getPackageSending();
					fw.append(getStringValue(val) + "\t");
					val = c.getChoosingByCost();
					fw.append(getStringValue(val) + "\t");
					val = c.getSTDepth();
					fw.append(getStringValue(val) + "\t");
					val = c.getWorstCommission();
					fw.append(getStringValue(val) + "\t");
					val = c.getAlgorithm();
					fw.append(getStringValue(val) + "\t");
					val = c.getAutoConfig();
					fw.append(getStringValue(val) + "\t");
					val = c.getFirstComplexSTResultOnly();
					fw.append(getStringValue(val) + "\t\n");
				}
			}
			fw.close();
			
			File roadTableFile = new File("roadGraphTable.txt");
			FileWriter fw2 = new FileWriter(roadTableFile);
			fw2.append("Road graph\n\n\ttrackFinder\tST\tchangeTime\tnotificationTime\tpredictor\n");
			for (Map.Entry<String, Configuration> conf: confs.entrySet()){
				Configuration c = conf.getValue();
				if (c.isPrint()){
	
					fw2.append(conf.getKey() + "\t");
					
					Object val = c.getTrackFinder();
					fw2.append(getStringValue(val) + "\t");
					val = c.getST();
					fw2.append(getStringValue(val) + "\t");
					val = c.getChangeTime();
					fw2.append(getStringValue(val) + "\t");
					val = c.getNotificationTime();
					fw2.append(getStringValue(val) + "\t");
					val = c.getPredictor();
					fw2.append(getStringValue(val) + "\t\n");
				}
			}
					
			
			fw2.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private String getStringValue(Object val){
		//if value is not available (is null), return '-'
		if (val == null)
			return "-";
		return val.toString(); 
	}
}
